//
// DirectText.cs
//  Gtk Window
//
// Author: 
//  Fredy Ramirez <framirez@pacificcoolwave.com>
//
// Copyright (C) 2011 Fredy Ramirez (http://www.pacificcoolwave.com)
//
//  Creation date: Sep 6, 2011
//  Last modification: 

/*
The MIT License

Copyright (C) 2011 Fredy Ramirez (http://www.pacificcoolwave.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */

using System;
using System.Collections.Generic;
using Gtk;

namespace Pcw.PcwTextEditor.PcwDocument
{
	/// <summary>
	/// Class used to pass text object direct to the Editor.
	/// </summary>
	public class DirectText : IDocumentType
	{
		private string str;
		private string ext;
		private string name;
		private TextBuffer buffer;

		private bool saved;
		
		private Dictionary<string, object> data = new Dictionary<string, object>();
		
		public event DocumentTypeEventHandler DocumentTypeCreate;
		public event DocumentTypeEventHandler DocumentTypeClose;
		public event DocumentTypeEventHandler DocumentTypeSave;
		public event DocumentTypeEventHandler DocumentTypeLoad;
		
		public bool IsNew {
			get  {
				return false;
			}
		}

		public bool Saved {
			get  {
				return saved;
			}
			set{
				saved = value;
			}
		}

		public string Name {
			get {
				return name;
			}
		}		
		
		public string Extension {
			get {
				return ext;
			}
		}		
		
		public string NameWithExtension {
			get {
				if (!String.IsNullOrEmpty(ext))
					return name + "." + ext;
				else
					return name;
			}
		}
		
		public string Path {
			get {
				return "";
			}
		}

		public string NameWithPath {
			get {
				return "";
			}
			set {
				;
			}
		}
		
		public Dictionary<string, object> Data {
			get {
				return data;
			}
		}
		
		/// <summary>
		/// The object text.
		/// </summary>		
		public string Text {
			get {
				return str;
			}
			set {
				str = value;
			}
		}
		
		public TextBuffer Buffer {
			set {
				buffer = value;
			}
		}		
		
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="name">
		///   A <see cref="string"/> name given to the object (It should be unique).
		/// </param>
		/// <param name="text">
		///   A <see cref="string"/> to be manipulated by the editor.
		/// </param>
		/// <param name="extension">
		///   A <see cref="string"/> extension for the object. It is used by addins (Ex for Highlighting).
		/// </param>
		public DirectText (string name, string text, string extension)
		{
			this.name = name;
			str = text;
			ext = extension;
			
			saved = true;
		}
		
		public bool Open() 
		{
			if (DocumentTypeCreate != null)
				DocumentTypeCreate(this);
			
			if (DocumentTypeLoad != null)
				DocumentTypeLoad(this);	
			
			return true;
		}
		
		public bool Close() 
		{
			if (DocumentTypeClose != null)
				DocumentTypeClose(this);	
			return true;
		}
		
		public bool Save() 
		{
			str = buffer.Text;
			saved = true;
			
			if (DocumentTypeSave != null)
				DocumentTypeSave(this);			
			
			return true;
		}
	}
}

